package com.azzaou.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.azzaou.dao.IMarchandiseDao;
import com.azzaou.model.CategorieMarchandise;
import com.azzaou.model.Marchandise;

public class MarchandiseDao extends HibernateDaoSupport implements IMarchandiseDao,	Serializable {

	private static final long serialVersionUID = 1L;

	@Override
	public List<Marchandise> getMarchandiseByCriteria(Marchandise emp) {

		StringBuffer requete = new StringBuffer("from Marchandise where 1=1 ");

		if (emp != null) {
			if (!StringUtils.isBlank(emp.getLibelle())) {
				System.out.println("Libelle => "+emp.getLibelle());
				requete.append(" AND libelle= :libelle ");
			}
//			if (!StringUtils.isBlank(emp.getPrenom())) {
//				System.out.println("Prenom => "+emp.getPrenom());
//				requete.append(" AND prenom= :prenom ");
//			}
//			if (emp.getSalaireJour() != 0) {
//				System.out.println("Salaire Journalier => "+emp.getSalaireJour());
//				requete.append(" AND salaireJour= :salaire ");
//			}
			// if (emp.getDep()!=null && emp.getDep()>0) {
			// System.out.println("Dept => "+emp.getDep());
			// requete.append(" AND departement= :dept ");
			// }
			// if (emp.getDateStart()!=null) {
			// System.out.println("Date Start => "+emp.getDateStart());
			// requete.append(" AND dateNaiss>=  :dateStart");
			// }
			// if (emp.getDateEnd() != null) {
			// System.out.println("Date End => " + emp.getDateEnd());
			// requete.append(" AND dateNaiss < :dateEnd ");
			// }
		}

		Query query = getSession().createQuery(requete.toString());

		if (emp != null) {
			if (!StringUtils.isBlank(emp.getLibelle())) {
				query.setString("nom", emp.getLibelle());
			}
//			if (!StringUtils.isBlank(emp.getPrenom())) {
//				query.setString("prenom", emp.getPrenom());
//			}
//			if (emp.getSalaireJour() != 0) {
//				query.setDouble("salaire Journalier", emp.getSalaireJour());
//			}
			// if (emp.getDep()!=null && emp.getDep()>0) {
			// query.setLong("dept", emp.getDep());
			// }
			// if (emp.getDateStart()!=null) {
			// query.setDate("dateStart", emp.getDateStart());
			// }
			// if (emp.getDateEnd() != null) {
			// query.setDate("dateEnd", emp.getDateEnd());
			// }
		}

		List<Marchandise> list = query.list();

		return list;
	}

	
	@Override
	public void saveOrUpdateMarchandise(Marchandise marchandise) {
		
		getHibernateTemplate().saveOrUpdate(marchandise);

	}

	
	@Override
	public void removeMarchandise(Marchandise marchandise) {
		
		getHibernateTemplate().delete(marchandise);

	}

	@Override
	public List<String> getAllMarchandisesByNames() {
		Query query = getSession().createQuery(" select nom from Marchandise ");
		List<String> list = query.list();
		return list;
	}


	@Override
	public List<String> getMarchandisesByNames(String begin) {
		String requet = "select e.nom from Marchandise e ";
		if(!StringUtils.isBlank(begin))
		{
			requet += "where e.nom like '"+begin+"%'";
		}
		Query query = getSession().createQuery(requet);
		List<String> list = query.list();
		return list;
	}


	
	@Override
	public List<Marchandise> getAllActifMarchandise() {
	
		List<Marchandise> marchandiseList = new ArrayList<Marchandise>();
	    Query query = getSession().createQuery(" FROM Marchandise where afficher = '1' ");
	   marchandiseList = query.list();
//	    marchandiseList = Collections.checkedList(getSession().createCriteria(CategorieMarchandise.class)
//	             .add(Restrictions.eq("categorieMarchandise.ID", "marchandise.categorie.id")).list(), Marchandise.class);
	    return marchandiseList;
		
	}


	@Override
	public List<CategorieMarchandise> getAllCategorie() {
		List<CategorieMarchandise> categList = new ArrayList<CategorieMarchandise>();
	    Query query = getSession().createQuery(" FROM CategorieMarchandise ");
	    categList = query.list();
	    
	    return categList;
	}


	@Override
	public void saveOrUpdateCategorie(CategorieMarchandise categorieMarchandise) {
		getHibernateTemplate().saveOrUpdate(categorieMarchandise);
	}


	@Override
	public void removeCategorie(CategorieMarchandise categorieMarchandise) {
		getHibernateTemplate().delete(categorieMarchandise);
	}

	
}